<template>
  <div class="maintenance-container">
    <div class="create-container">
      <el-button type="primary" @click="addRule">新增运维历史</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="管道编号" prop="pipeNumber" />
        <el-table-column label="维修员编号" prop="workerNumber" />
        <el-table-column label="联系电话" prop="phoneNumber" />
        <el-table-column label="收费金额" prop="chargeFee" />
        <el-table-column label="申请日期" prop="date" />
        <el-table-column label="申请时间" prop="time" />

        <el-table-column label="维修方式" prop="maintenanceMethod">
          <template #default="scope">
            {{ formMaintenanceMethod(scope.row.maintenanceMethod) }}
          </template>
        </el-table-column>

      </el-table>
      <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        layout="sizes, prev, pager, next"
        :total="total"
        :page-sizes="[10, 20, 30, 40, 50]"
        :current-page="params.page"
        :page-size="params.pageSize"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    </div>
    <!-- 弹框 -->
<el-dialog :visible="dialogVisible" width="680px" title="添加记录" :close-on-click-modal="false"
 @close="closeDialog">
  <!-- 表单接口 -->
  <div class="form-container">
    <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="top">
      <el-form-item label="管道编号" prop="pipeNumber">
        <el-input v-model="addForm.pipeNumber" />
      </el-form-item>
      <el-form-item label="维修员编号" prop="workerNumber">
        <el-input v-model="addForm.workerNumber" />
      </el-form-item>

      <el-form-item label="维修方式" prop="maintenanceMethod">
        <el-radio-group v-model="addForm.maintenanceMethod" size="small">
          <el-radio label="pipelineInspection" border>管道检测</el-radio>
          <el-radio label="rushPipe" border>管道清洗</el-radio>
          <el-radio label="replacePipeline" border>管道更换</el-radio>
        </el-radio-group>
      </el-form-item>

      <div class="flex-container">
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="addForm.phoneNumber" />
        </el-form-item>
        <el-form-item label="收费金额" prop="chargeFee">
          <el-input v-model="addForm.chargeFee" />
        </el-form-item>
      </div>
    </el-form>
  </div>
  <template #footer>
    <el-button size="mini" @click="closeDialog">取 消</el-button>
    <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
  </template>
</el-dialog>
  </div>
</template>

<script>
import { getOperationMaintenanceListAPI, addOperationMaintenanceAPI } from '@/api/history'
import { utils, writeFileXLSX } from 'xlsx'
export default {
  name: 'Building',
  data() {
    return {
      ruleList: [], // 规则列表
      // 请求参数
      params: {
        page: 1,
        pageSize: 10
      },
      // 规则总数
      total: 0,
      dialogVisible: false,
      addForm: {
      pipeNumber: '', // 管道编号
      workerNumber: '', // 维修员编号
      maintenanceMethod: 'pipelineInspection', // 维修方式 pipelineInspection / rushPipe / replacePipeline
      chargeFee: null,
      phoneNumber: null
    }, // 添加对象
    addFormRules: {
      pipeNumber: [
        {
          required: true,
          message: '请输入管道编号',
          trigger: 'blur'
        }
      ],
      workerNumber: [
        {
          required: true,
          message: '请输入维修员编号',
          trigger: 'blur'
        }
      ],
      maintenanceMethod: [
        {
          required: true,
          message: '请选择维修方式',
          trigger: 'blur'
        }
      ],
      phoneNumber: [
        {
          required: true,
          message: '请选择联系电话',
          trigger: 'blur'
        },{
          pattern:/^1[3-9]\d{9}$/,
          message:"手机号格式错误",
          trigger: 'blur'
        }
      ],
      chargeFee: [
        {
          required: true,
          message: '请选择收费金额',
          trigger: 'blur'
        },{
          pattern: /^\d+$/,
          message: "请输入金额",
          trigger: 'blur'
        }
      ]
    } 
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    confirmAdd(){
      this.$refs.addForm.validate(async flag =>{
        if(!flag) return
        await addOperationMaintenanceAPI(this.addForm)
        this.$message.success('添加成功！')
        this.getList()
        this.closeDialog()
      })
    },
    closeDialog(){
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    addRule(){
      this.dialogVisible = true
    },
    formMaintenanceMethod(maintenanceMethod) {
      const MAP = {
        pipelineInspection: '管道检测',
        rushPipe: '管道清洗',
        replacePipeline: '管道更换'
      }
      return MAP[maintenanceMethod]
    },
    async exportExcel() {
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表 要求一个对象数组格式
      // 1. 获取当前要导出的table数据
      const res = await getOperationMaintenanceListAPI(this.params)
      // 2. 按照需求调整数据的顺序
      const tableHeaderKeys = ['pipeNumber', 'workerNumber', 'phoneNumber', 'chargeFee', 'maintenanceMethod','date' ,'time']
      const tableHeaderValues = ['管道编号', '维修员编号', '联系电话', '收费金额', '维修方式', '申请日期','申请时间']
      // 以excel表格的顺序调整后端数据
      const sheetData = res.data.rows.map((item) => {
        const newObj = {} // 存放正确顺序的key-value
        tableHeaderKeys.forEach(key => {
          if (key === 'maintenanceMethod') {
            newObj[key] = this.formMaintenanceMethod(item[key])
          } else {
            newObj[key] = item[key]
          }
        })
        return newObj
      })
      const worksheet = utils.json_to_sheet(sheetData)
      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, '运维历史.xlsx')
    },
    indexMethod(index){
      return (this.params.page -1)*this.params.pageSize + index + 1
    },
    async getList() {
      const res = await getOperationMaintenanceListAPI(this.params)
      this.ruleList = res.data.rows
      this.total = res.data.total
    },
        // 分页
    handleCurrentChange(val) {
      this.params.page = val
      this.getList()
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.maintenance-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
